<?php

namespace frontend\modules\comment\models\search;

use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use backend\modules\comment\models\Comment;

/**
 * CommentSearch represents the model behind the search form about `backend\modules\comment\models\Comment`.
 */
class CommentSearch extends Comment
{
    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['comment_id', 'goods_id', 'attrcat_id', 'email', 'user_name', 'content', 'ip_address', 'parent_id', 'user_id', 'comment_img_id'], 'safe'],
            [['comment_rank', 'add_time', 'is_niming'], 'integer'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function scenarios()
    {
        // bypass scenarios() implementation in the parent class
        return Model::scenarios();
    }

    /**
     * Creates data provider instance with search query applied
     *
     * @param array $params
     *
     * @return ActiveDataProvider
     */
    public function search($params)
    {
        $query = Comment::find();

        // add conditions that should always apply here

        $dataProvider = new ActiveDataProvider([
            'query' => $query,
        ]);

        $this->load($params);

        if (!$this->validate()) {
            // uncomment the following line if you do not want to return any records when validation fails
            // $query->where('0=1');
            return $dataProvider;
        }

        // grid filtering conditions
        $query->andFilterWhere([
            'comment_rank' => $this->comment_rank,
            'add_time' => $this->add_time,
            'is_niming' => $this->is_niming,
        ]);

        $query->andFilterWhere(['like', 'comment_id', $this->comment_id])
            ->andFilterWhere(['like', 'goods_id', $this->goods_id])
            ->andFilterWhere(['like', 'attrcat_id', $this->attrcat_id])
            ->andFilterWhere(['like', 'email', $this->email])
            ->andFilterWhere(['like', 'user_name', $this->user_name])
            ->andFilterWhere(['like', 'content', $this->content])
            ->andFilterWhere(['like', 'ip_address', $this->ip_address])
            ->andFilterWhere(['like', 'parent_id', $this->parent_id])
            ->andFilterWhere(['like', 'user_id', $this->user_id])
            ->andFilterWhere(['like', 'comment_img_id', $this->comment_img_id]);

        return $dataProvider;
    }
}
